/*
 * @Description:
 * @Version: 1.0
 * @Autor:
 * @Date: 2023-08-08 10:09:33
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-11-22 18:21:34
 */
const path = require('path');
// import path from 'path';
const {NodeSSH} = require('node-ssh'); //引入node-ssh
// import {NodeSSH} from 'node-ssh'; //引入node-ssh
const ssh = new NodeSSH();
const configs = {
	host: '101.43.94.70',
	username: 'root',
	password: '1Q2w3e4r..?',
	port: 22,
	serverPath: '/etc/nginx/weixinh5',
	distPath: path.resolve(__dirname, 'weixinh5'),
};
//上传到服务器
uploadfile();
function uploadfile() {
	console.log('开始上传');
	ssh
		.connect({
			//configs存放的是连接远程机器的信息
			host: configs.host,
			username: configs.username,
			password: configs.password,
			port: configs.port, //ssh连接默认在22端口
		})
		.then(function () {
			console.log('ssh连接成功:', configs.host);
			console.log('上传中...');
			console.log(__dirname, configs.distPath, configs.serverPath);
			//上传网站的发布包至configs中配置的远程服务器的指定地址
			ssh
				.putDirectory(configs.distPath, configs.serverPath, {
					tick: (localPath, remotePath, error) => {
						if (error) {
							console.error('Error:', error);
						} else {
							console.log(`Uploaded: ${localPath} to ${remotePath}`);
						}
					},
				})
				.then(function () {
					console.log('上传成功');
					ssh.dispose();
					process.exit(0);
				})
				.catch((err) => {
					console.log('上传出错:', err);
					ssh.dispose();
					process.exit(0);
				});
		})
		.catch((err) => {
			console.log('ssh连接失败:', err);
			ssh.dispose();
			process.exit(0);
		});
}
